package top.lixunda.ecommerce.server.good.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.common.validate.XumValid;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.good.api.entity.vo.GoodConvertVO;
import top.lixunda.ecommerce.server.good.api.entity.db.UserCarDO;
import top.lixunda.ecommerce.server.good.api.entity.dto.AddUserCarDTO;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.UserCarQueryDTO;
import top.lixunda.ecommerce.server.good.api.entity.vo.UserCarConvertGoodVO;
import top.lixunda.ecommerce.server.good.api.entity.vo.UserCarVO;
import top.lixunda.ecommerce.server.good.api.controller.IUserCarController;
import top.lixunda.ecommerce.server.good.service.IUserCarService;
import top.lixunda.ecommerce.server.good.service.impl.GoodDetailServiceImpl;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Xumda
 * @version time:2020/1/10 12:56
 */
@Slf4j
@RestController
public class UserCarController extends BaseController implements IUserCarController {

    private final IUserCarService userCarService;

    private final GoodDetailServiceImpl goodDetailService;

    @Autowired
    public UserCarController(IUserCarService userCarService,
                             GoodDetailServiceImpl goodDetailService) {
        this.userCarService = userCarService;
        this.goodDetailService = goodDetailService;
    }

    /**
     * 添加购物车
     *
     * @param addUserCarDTO 商品数据，包括商品id和商品数量
     * @return 是否添加成功
     */
    @Override
    public MessageObject<Boolean> addToCar(@XumValid AddUserCarDTO addUserCarDTO) {
        Boolean success = userCarService.addToCar(addUserCarDTO);
        if (success) {
            return getSuccessMessageObject("添加购物车成功", true);
        } else {
            return getErrorMessageObject("添加购物车失败", false);
        }
    }

    /**
     * 根据id移除购物车数据
     *
     * @param ids 商品在购物车中的id列表
     * @return 移除的数量
     */
    @Override
    public MessageObject<Integer> removeCar(List<Integer> ids) {
        if (ObjectValidators.isEmpty(ids)) {
            return getErrorMessageObject("参数不能为空");
        }
        int count = userCarService.removeCar(ids);
        if (count != ids.size()) {
            throw getAppException("移除购物车失败");
        }
        return getSuccessMessageObject("移除购物车成功", count);
    }

    /**
     * 分页条件查询购物车数据
     *
     * @param pageDTO 分页数据及条件查询数据
     * @return 购物车分页数据
     */
    @Override
    public MessageObject<IPage<UserCarVO>> queryCarGoodsByPage(
            @XumValid PageDTO<UserCarDO, UserCarQueryDTO> pageDTO) {
        IPage<UserCarVO> resultPage = userCarService.queryCarGoodsByPage(pageDTO);
        return getSuccessMessageObject("获取购物车数据成功", resultPage);
    }

    /**
     * 查询当前登陆用户的购物车，以时间排序
     *
     * @param pageDTO 分页数据及条件查询数据
     * @return 分页信息和分页数据
     */
    @Override
    public MessageObject<IPage<UserCarConvertGoodVO>> queryCarConvertGoodsByPage(
            @XumValid PageDTO<UserCarDO, UserCarQueryDTO> pageDTO) {
        // 查询本身的数据
        IPage<UserCarVO> page = userCarService.queryCarGoodsByPage(pageDTO);
        IPage<UserCarConvertGoodVO> resultPage = new Page<>();
        // 提取商品id
        List<String> ids = page.getRecords().stream()
                .map(UserCarVO::getGoodId)
                .map(String::valueOf)
                .collect(Collectors.toList());
        if (ObjectValidators.isEmpty(ids)) {
            resultPage.setRecords(Collections.emptyList());
            return getSuccessMessageObject("获取数据成功", resultPage);
        }
        // 查询详细信息
        List<GoodConvertVO> list = goodDetailService.listConvertByIds(ids);
        Map<Integer, GoodConvertVO> map = new HashMap<>(list.size());
        list.forEach(item -> map.put(item.getId(), item));
        // 转换为 UserCarConvertGoodVO
        List<UserCarConvertGoodVO> resultList = page.getRecords().stream()
                .map(item -> {
                    UserCarConvertGoodVO vo = new UserCarConvertGoodVO();
                    BeanUtils.copyProperties(item, vo);
                    vo.setGoodConvertVO(map.get(item.getGoodId()));
                    return vo;
                })
                .collect(Collectors.toList());
        // 构造IPage
        resultPage.setRecords(resultList);
        return getSuccessMessageObject("获取数据成功", resultPage);
    }

}
